/**
 * 收银中心管理初始化
 */
var TOrderFee = {
    id: "REITable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1,
    lock: false
};

/**
 * 初始化表格的列
 */
TOrderFee.initColumn = function () {
    return [

        {title: '单号', field: 'original_num', visible: true, align: 'center', valign: 'middle'},
        {title: '货物名称', field: 'goods_name', visible: true, align: 'center', valign: 'middle'},
        {title: '件数', field: 'goods_piece', visible: true, align: 'center', valign: 'middle'},
        {title: '提付', field: 'payment', visible: true, align: 'center', valign: 'middle'},
        {title: '代收货款', field: 'collection', visible: true, align: 'center', valign: 'middle'},
        {title: '落地费', field: 'extra_money', visible: true, align: 'center', valign: 'middle'},
        {title: '送货费', field: 'shf', event: 'a', visible: true, align: 'center', valign: 'middle'},
        {title: '搬运费', field: 'byf', event: 'b', visible: true, align: 'center', valign: 'middle'},
        {title: '仓储费', field: 'ccf', event: 'c', visible: true, align: 'center', valign: 'middle'},
        {title: '车费', field: 'cf', event: 'd', visible: true, align: 'center', valign: 'middle'},
        {title: '通知发货', field: 'notice', visible: true, align: 'center', valign: 'middle'},
        {title: '回单', field: 'receipt', visible: true, align: 'center', valign: 'middle'},
        {title: '配送结果', field: 'isfail', visible: true, align: 'center', valign: 'middle', templet: checkboxTpl}

    ];
};

/**
 * 关闭此对话框
 */
TOrderFee.close = function () {

    parent.layer.close(window.parent.TTrain.layerIndex);
}
/**
 * 提交修改
 */
TOrderFee.editSubmit = function () {

    // var w = window.open();
    //提交信息
    if(!TOrderFee.lock) {
        //防止多次点击
        TOrderFee.lock = true; // 锁定
        var ajax = new $ax(Feng.ctxPath + "/tOrder/addAndTrian", function (data) {
            Feng.success("操作成功!");
            window.parent.aTable.refresh();
            TOrderFee.close();
        }, function (data) {
            Feng.error("操作失败!" + data.responseJSON.message + "!");
        });
        ajax.set("driver_name");
        ajax.set("ids");
        ajax.set("phone");
        ajax.set("cf");
        ajax.set("car_no", $('#carNo').val());
        ajax.set("temOrderFeeS", JSON.stringify(vDate));
        ajax.set("ecIds", isEdits.join(","));
        ajax.start();
    }
}


Array.prototype.remove = function (val) {
    var index = this.indexOf(val);
    if (index > -1) {
        this.splice(index, 1);
    }
};
var temOrderFeeS = new Array();

function mUpdata(obj, type, field) {


    var data = obj.data;
    var mTitle;
    var mValue = 0;
    if (type == 'a') {
        mValue = data.shf;
        mTitle = "送货费";
    } else if (type == 'b') {
        mValue = data.byf;
        mTitle = "搬用费";
    } else if (type == 'c') {
        mValue = data.ccf;
        mTitle = "仓储费";
    } else if (type == 'd') {
        mValue = data.cf;
        mTitle = "车费";
    } else if (type == 'e') {
        mValue = data.fee_remark;
        mTitle = "备注";
    }
    //这里一般是发送修改的Ajax请求

    //同步更新表格和缓存对应的值
    layer.prompt({
        formType: 2
        , title: '修改' + mTitle + '金额'
        , value: mValue
    }, function (value, index) {
        var mSum = 0;
        var fee_id = "";
        if (type == 'a') {
            mSum = (parseFloat(data.payment) + parseFloat(data.collection) + (parseFloat(data.byf) + parseFloat(data.ccf) + parseFloat(data.extra_money) + parseFloat(value)));
            fee_id = "27";
            obj.update({
                suma: mSum,
                shf: value.trim()
            });
            obj.data.shf = value.trim();
            obj.data.mSum = mSum;

        } else if (type == 'b') {
            mSum = (parseFloat(data.payment) + parseFloat(data.collection) + (parseFloat(data.shf) + parseFloat(data.extra_money) + parseFloat(value)));
            obj.update({
                suma: mSum,
                byf: value.trim()
            });
            obj.data.byf = value.trim();
            obj.data.mSum = mSum;
            fee_id = "28";
        } else if (type == 'c') {
            mSum = (parseFloat(data.payment) + parseFloat(data.collection) + (parseFloat(data.shf) + parseFloat(data.byf) + parseFloat(data.extra_money) + parseFloat(value)));
            fee_id = "29";
            obj.update({
                suma: mSum,
                ccf: value.trim()
            });
            obj.data.ccf = value.trim();
            obj.data.mSum = mSum;
        } else if (type == 'd') {
            mSum = (parseFloat(data.payment) + parseFloat(data.collection) + (parseFloat(data.shf) + parseFloat(data.byf) + parseFloat(data.extra_money) + parseFloat(data.ccf)));
            fee_id = "30";
            obj.update({
                suma: mSum,
                cf: value.trim()
            });
            obj.data.cf = value.trim();
            obj.data.mSum = mSum;
        } else if (type == 'e') {
            obj.data.fee_remark = value.trim();
            obj.update({
                fee_remark: value.trim()
            });
        }


        upMdate(obj.data, mSum);

        layer.close(index);


    });
}

function upMdate(oData, oSum) {
    var mSum = 0;
    var uSum = 0;
    for (var i = 0; i < this.vDate.length; i++) {
        if (this.vDate[i].id == oData.id) {
            oData.suma = oSum;
            this.vDate[i] = oData;
            mSum = mSum.add(parseFloat(oData.suma));
            uSum = uSum.add(parseFloat(oData.cf));
        } else {
            mSum = mSum.add(parseFloat(this.vDate[i].suma));
            uSum = uSum.add(parseFloat(this.vDate[i].cf));
        }
    }
    $('#xj').text(mSum - uSum);
    $('#cf').val(uSum);
}

function cfSum(mVal) {
    if (!isNaN(mVal)) {

        var tDate = new Array();

        var a = Math.floor(eval(mVal).div(eval(this.vDate.length-isEdits.length)));
        var b = eval(mVal).sub(eval(a).mul(this.vDate.length - isEdits.length-1));

        for (var i = 0; i < this.vDate.length; i++) {
            if (!isEdits.contains(this.vDate[i].id)) {
                this.vDate[i].isfail=1;
                tDate.push(this.vDate[i]);
            }else{
                this.vDate[i].cf=0;
                this.vDate[i].isfail=0;
            }
        }

        for (var i = 0; i < tDate.length; i++) {
            if (i == tDate.length - 1) {
                tDate[i].cf = b;
            } else {
                tDate[i].cf = a;
            }
        }
        for (var i = 0; i < this.vDate.length; i++) {
            for (var j = 0; j < tDate.length; j++) {
                if (this.vDate[i].id == tDate[j].id) {
                    this.vDate[i] = tDate[j];
                }
            }
        }

        mTable.reload(TOrderFee.id, {
            url: '',
            data: vDate
        })
    }

}

Array.prototype.contains = function (needle) {
    for (i in this) {
        if (this[i] == needle) return true;
    }
    return false;
}

var vDate = null;

function collectSum(mDate) {
    this.vDate = mDate.data;
    var uSum = 0;
    var mSum = 0;
    for (var i = 0; i < mDate.data.length; i++) {
        uSum = uSum.add(mDate.data[i].cf);
        mSum = mSum.add(parseFloat(mDate.data[i].suma));
    }
    $('#cf').val(uSum);

}

var aTable = null
var mTable = null;
var isEdits = new Array();
$(function () {

    var defaultColunms = TOrderFee.initColumn();
    aTable = new LYTable(TOrderFee.id, "/tOrder/mDataList?ids=" + $('#ids').val(), defaultColunms);
    layui.use('table', function () {
        mTable = layui.table;
        var form = layui.form;

        TOrderFee.table = aTable.initCallBack(mTable, collectSum);

        mTable.on('tool(' + TOrderFee.id + ')', function (obj) {
            mUpdata(obj, obj.event, obj.field);
        })

        form.on('switch(isEdit)', function (obj) {
            if (!obj.elem.checked) {
                isEdits.push(this.value);
            } else {
                isEdits.remove(this.value);
            }
            cfSum($('#cf').val());
        });

    })


});
